//
//  CustomOrder.swift
//  Composite
//
//  Created by LY on 2017/10/26.
//  Copyright © 2017年 DesignPatterns. All rights reserved.
//

import Foundation
class CustomOrder{
    let customer:String
    let parts : [Part]
    let compositeParts:[CompositePart]
    init(customer:String,part:[Part],compositePart:CompositePart...) {
        self.customer = customer
        parts = part
        compositeParts = compositePart
    }
    var totalPrice:Float{
        let partReducer = {
            (subtotal:Float,part:Part) -> Float in
            return subtotal + part.price
        }
        let total = parts.reduce(0, partReducer);
        let compesiteTotal = compositeParts.reduce(0, { (subitem, cpart:CompositePart) -> Float in
            return cpart.part.reduce(subitem, partReducer);
        })
        return total + compesiteTotal
    }
    /// 输出所有数据
    func printDetails(){
        print("Order for \(customer): Cost: \(formatCurrencyString(number: totalPrice))")
    }
    func formatCurrencyString(number:Float) -> String{
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        return formatter.string(for: number) ?? ""
    }
}
